const { ipcRenderer } = require('electron');

// 获取DOM元素
const opacitySlider = document.getElementById('opacity');
const opacityValue = document.getElementById('opacity-value');
const backgroundColorPicker = document.getElementById('background-color');
const fontSizeSelect = document.getElementById('font-size');
const fontColorPicker = document.getElementById('font-color');
const alwaysTopCheckbox = document.getElementById('always-top');
const clickThroughCheckbox = document.getElementById('click-through');
const saveBtn = document.getElementById('save-settings');
const cancelBtn = document.getElementById('cancel-settings');
const dbPathInput = document.getElementById('db-path');
const browseDbPathBtn = document.getElementById('browse-db-path');
// 分享信息设置DOM元素
const shareNameInput = document.getElementById('share-name');
const shareDepartmentInput = document.getElementById('share-department');
const sharePositionInput = document.getElementById('share-position');

// 更新透明度显示值
opacitySlider.addEventListener('input', () => {
    opacityValue.textContent = opacitySlider.value;
});

// 浏览数据库目录
browseDbPathBtn.addEventListener('click', () => {
    ipcRenderer.send('browse-db-path');
});

// 监听数据库路径选择结果
ipcRenderer.on('db-path-selected', (event, path) => {
    if (path) {
        dbPathInput.value = path;
    }
});

// 保存设置
saveBtn.addEventListener('click', () => {
    const settings = {
        opacity: parseFloat(opacitySlider.value),
        backgroundColor: backgroundColorPicker.value,
        fontSize: fontSizeSelect.value,
        fontColor: fontColorPicker.value,
        alwaysOnTop: alwaysTopCheckbox.checked,
        clickThrough: clickThroughCheckbox.checked,
        dbPath: dbPathInput.value,
        // 分享信息设置
        shareInfo: {
            name: shareNameInput.value,
            department: shareDepartmentInput.value,
            position: sharePositionInput.value
        }
    };
    
    // 保存设置到本地存储
    localStorage.setItem('widgetSettings', JSON.stringify(settings));
    
    // 发送消息到主进程应用设置
    ipcRenderer.send('apply-settings', settings);
});

// 取消设置
cancelBtn.addEventListener('click', () => {
    ipcRenderer.send('close-settings');
});

// 监听设置应用成功的消息
ipcRenderer.on('settings-applied', () => {
    alert('设置已保存！');
    // 关闭设置窗口，触发close-settings事件，确保设置正确应用
    ipcRenderer.send('close-settings');
});

// 加载已保存的设置
window.addEventListener('DOMContentLoaded', () => {
    const savedSettings = localStorage.getItem('widgetSettings');
    if (savedSettings) {
        const settings = JSON.parse(savedSettings);
        
        opacitySlider.value = settings.opacity;
        opacityValue.textContent = settings.opacity;
        backgroundColorPicker.value = settings.backgroundColor;
        fontSizeSelect.value = settings.fontSize;
        fontColorPicker.value = settings.fontColor;
        alwaysTopCheckbox.checked = settings.alwaysOnTop;
        clickThroughCheckbox.checked = settings.clickThrough;
        
        // 加载数据库路径设置
        if (settings.dbPath) {
            dbPathInput.value = settings.dbPath;
        }
        
        // 加载分享信息设置
        if (settings.shareInfo) {
            shareNameInput.value = settings.shareInfo.name || '';
            shareDepartmentInput.value = settings.shareInfo.department || '';
            sharePositionInput.value = settings.shareInfo.position || '';
        }
    }
    
    // 初始化时获取当前数据库路径
    ipcRenderer.send('get-current-db-path');
});

// 监听当前数据库路径响应
ipcRenderer.on('current-db-path', (event, path) => {
    if (path && !dbPathInput.value) {
        dbPathInput.value = path;
    }
});

// 添加键盘事件支持
document.addEventListener('keydown', (e) => {
    if (e.key === 'Escape') {
        cancelBtn.click();
    }
});